{layout name="layout/header" /}
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="root/js/plugins/fancybox/jquery.fancybox.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<div class="wrapper wrapper-content animated fadeInRight" id="app">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{$title}</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" id="form">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名</label>
                            <div class="col-sm-10">
                                <input v-if="id==0" type="text" class="form-control" v-model="username"  name="username" placeholder="用户名">
                                <p v-else class="form-control-static">{{username}}</p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">登录密码</label>
                            <div class="col-sm-10">
                                <input type="password" name="password" v-model="password" id="password" class="form-control">
                                <span v-if="id==0">登录密码不能少于6位</span>
                                <span v-else>登录密码不能少于6位，如果不修改密码可以不填写此项</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">重复密码</label>
                            <div class="col-sm-10">
                                <input type="password" name="confirm_password" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-10">
                                <div class="radio i-checks">
                                    <label><input type="radio" v-model="enable" value="1" name="enable"> <i></i>正常</label>
                                    <label><input type="radio" v-model="enable" value="0" name="enable"> <i></i>禁用</label>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <a class="btn btn-primary" @click="submit" type="submit">保存内容</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script src="root/js/plugins/validate/jquery.validate.min.js"></script>
<script src="root/js/plugins/validate/messages_zh.min.js"></script>
<script src="root/js/plugins/fancybox/jquery.fancybox.js"></script>

<script>
    var data = {$data};

    var url = "{$url}";

    if (data == 1) {
        data = {
            id: 0,
            username:'',
            password:'',
            remark:'',
            enable:1,
            level:0,
            site_id:"{$site.id}",
        }
    }

    //1
    new Vue({
        el: "#app",
        data: data,
        created:function(){
            var _this = this;

        },
        methods: {
            submit:function () {
                var _this = this;
                $.validator.addMethod("unique",function(value,element,params){
                    var a = 0;
                    $.ajax({
                        url:"{:url('ajax_unique_username')}",
                        type:"post",
                        dataType:"json",
                        async:false,
                        data:{username:value},
                        success:function (res) {
                            if(res.code == 1){
                                a = 1;
                            }
                        }
                    });

                    if(a == 0){
                        return true;
                    }else {
                        return false;
                    }
                },"用户名已存在");
                var rules = {
                    rules: {
                        username:{unique:true,required:true},
                        password: {
                            required: true,
                            minlength: 6
                        },
                        confirm_password: {
                            required: true,
                            minlength: 6,
                            equalTo: "#password"
                        },
                        role_id:{
                            min:1,
                        }
                    },
                    messages:{
                        role_id:{
                            min:'请选择一个用户组',
                        }
                    }
                }
                if(_this.id != 0){
                    rules = {
                        rules: {
                            username:{unique:true,required:true},
                            password: {
                                minlength: 6
                            },
                            confirm_password: {
                                minlength: 6,
                                equalTo: "#password"
                            },
                        },
                        messages:{
                            role_id:{
                                min:'请选择一个用户组',
                            }
                        }
                    }
                }

                $("#form").validate(rules)
                if($("#form").valid()) {

                    $.post(url,data,function (res) {
                        layer.msg(res.msg);

                        if(res.code==1){
                            setTimeout(function () {
                                window.location.reload()
                            },1500)
                        }
                    },'json')
                }

            }
        },
        mounted: function () {
            checks();
            $('.fancybox').fancybox({
                openEffect: 'none',
                closeEffect: 'none'
            });
        }
    });
</script>

<script>
    $('input[name="enable"]').on('ifChecked', function (event) {
        data.enable = $(this).val()
    });

</script>
